<template>
  <div class="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <div class="title">前端脚手架</div>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" size="large" auto-complete="off" placeholder="账号">
          <template #prefix>
            <svg-icon icon-class="user" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          show-password
          @keyup.enter="handleLogin"
        >
          <template #prefix>
            <svg-icon icon-class="password" class="el-input__icon input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <div class="w-full flex flex-row items-center justify-between">
          <el-input
            v-model="loginForm.code"
            size="large"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <svg-icon icon-class="validCode" class="el-input__icon input-icon" />
            </template>
          </el-input>
          <img :src="codeUrl" @click="getCode" class="login-code-img" alt="" />
        </div>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width: 100%"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
      <div class="flex flex-row items-center gap-3 pr-6 pb-4">
        <!-- <el-link type="primary" @click="getYzyCode">粤政易登录</el-link> -->
        <el-link type="primary" @click="goToSso">市可信登录</el-link>
      </div>
    </el-form>
  </div>
</template>

<script setup>
  import { ref } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import { getCodeImg } from '@/api/login';
  import queryString from 'query-string';
  import useUserStore from '@/store/modules/user';

  const userStore = useUserStore();
  const route = useRoute();
  const router = useRouter();

  const loginRef = ref(null);
  const loginForm = ref({
    username: '',
    password: '',
    code: '',
    uuid: '',
  });

  const loginRules = {
    username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
    password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
    code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
  };

  const codeUrl = ref('');
  const loading = ref(false);
  const captchaEnabled = ref(true);

  function handleLogin() {
    loginRef.value.validate((valid) => {
      if (valid) {
        loading.value = true;
        userStore
          .login(loginForm.value)
          .then(() => {
            const redirect = route.query?.redirect;
            if (redirect) {
              const fakePath = `https://example.com${redirect}`;
              const url = new URL(fakePath);
              const query = queryString.parse(url.search);
              router.replace({ path: redirect, query });
            } else {
              router.replace({ path: '/' });
            }
          })
          .catch(() => {
            loading.value = false;
            if (captchaEnabled.value) {
              getCode();
            }
          });
      }
    });
  }

  function getCode() {
    getCodeImg().then((res) => {
      captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
      if (captchaEnabled.value) {
        codeUrl.value = 'data:image/gif;base64,' + res.img;
        loginForm.value.uuid = res.uuid;
      }
    });
  }

  function goToSso() {
    // window.location.search 为 ?redirect=/ft-digital/received
    const callbackUrl = window.location.origin + '/skx-callback' + window.location.search;
    location.href =
      'https://sso.szft.gov.cn/sso/login?authUrl=' + window.encodeURIComponent(callbackUrl);
  }

  getCode();
</script>

<style lang="scss" scoped>
  .login {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background-image: url('../assets/images/login-background.jpg');
    background-size: cover;

    .login-form {
      width: 400px;
      padding: 25px 25px 5px;
      border-radius: 6px;
      background: #fff;

      .title {
        margin: 0 auto 30px;
        color: #707070;
        font-size: 18px;
        font-weight: 500;
        text-align: center;
      }

      .el-input {
        height: 40px;
      }

      .input-icon {
        width: 14px;
        height: 40px;
        margin-left: 0;
      }

      .login-code-img {
        height: 40px;
      }
    }
  }
</style>
